define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'reserve/index' + location.search,
                    add_url: 'reserve/add',
                    edit_url: 'reserve/edit',
                    del_url: 'reserve/del',
                    multi_url: 'reserve/multi',
                    import_url: 'reserve/import',
                    table: 'reserve',
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='store_id']", form).addClass("selectpage").data("source", "store/index").data("primaryKey", "id").data("field", "store_name");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            var payWays = {"1":__('Pay_way 1'),"2":__('Pay_way 2'),"3":__('Pay_way 3'),"4":__('Pay_way 4'), "5":__('Pay_way 5')}

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'trade_no', title: "预约编号", operate: 'LIKE'},
                        {field: 'user.username', title: __('Username'), operate: 'LIKE'},
                        {field: 'user.nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'user_extend.name', title: '姓名', operate: false},
                        {field: 'user.mobile', title: __('Mobile'), operate: false},
                        {field: 'store_id', title: __('Store_id'), operate: '=', visible: false},
                        {field: 'store.store_name', title: __('Store_id'), operate: false, formatter: Controller.api.showstore},
                        {field: 'room.room_name', title: __('Room_id'), operate: false, formatter: Controller.api.showroom},
                        {field: 'starttime', title: __('Starttime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'endtime', title: __('Endtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'fee', title: __('Fee'), operate: 'between'},
                        {field: 'pay_way', title: __('Pay_way'), searchList: payWays, 
                            formatter: function(value, row, index) {
                                if (value == '5') {
                                    if (row.card_id) {
                                        return '卡券支付+微信支付'
                                    }
                                    if (row.balance_id) {
                                        return '余额支付+微信支付'
                                    }
                                }
                                return payWays[value];
                            }
                        },
                        {field: 're_status', title: __('Re_status'), searchList: {"0":__('Re_status 0'),"1":__('Re_status 1'),"2": __("Re_status 2")}, formatter: Table.api.formatter.normal},
                        /* 
                        {field: 'card_id', title: __('Card_id'), operate: false},
                        {field: 'balance_id', title: __('Balance_id'), operate: false},
                        {field: 'transaction_id', title: __('Transaction_id'), operate: false},
                        */
                        {field: 'total_fee', title: __('总金额'), operate: false},
                        {field: 'card_fee', title: __('卡券支付'), operate: false},
                        {field: 'balance_fee', title: __('余额支付'), operate: false},
                        {field: 'transaction_fee', title: __('直接支付'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'cancel_button',
                                    text: __('取消预约'),
                                    classname: 'btn btn-xs btn-info btn-ajax',
                                    icon: 'fa fa-check-square',
                                    confirm: '确定取消此预约吗？',
                                    url: function (row) {
                                        return Fast.api.fixurl('reserve/ajaxCancelReserve?id=' + row.id)
                                    },
                                    success: function (data, ret) {
                                        // Layer.alert(ret.msg);
                                        table.bootstrapTable('refresh');
                                        //如果需要阻止成功提示，则必须使用return false;
                                        // return false;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                            ],
                            formatter: function(value, row, index){
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                if (row.re_status == 2){
                                    $(table).data("operate-cancel_button", null);
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ],
                searchFormVisible: true,
                search: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
